package com.example.neusoft.yuejian.Activity;



import android.content.Intent;
import android.content.SharedPreferences;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;


import com.example.neusoft.yuejian.Base.BaseActivity;
import com.example.neusoft.yuejian.Base.BaseListener;
import com.example.neusoft.yuejian.Base.BaseModel;
import com.example.neusoft.yuejian.Bean.CancelBean;
import com.example.neusoft.yuejian.Bean.ClubBean;
import com.example.neusoft.yuejian.Bean.ClubDetailBean;
import com.example.neusoft.yuejian.Bean.GuanzhuBean;
import com.example.neusoft.yuejian.Bean.LessonBean;
import com.example.neusoft.yuejian.Bean.TrainerBean;
import com.example.neusoft.yuejian.Bean.videolist;

import com.example.neusoft.yuejian.R;
import com.squareup.picasso.Picasso;


import java.io.Serializable;
import java.util.List;

import retrofit2.Call;

public class ClubActivity extends BaseActivity implements View.OnClickListener {


    private ClubDetailBean cbean;
    private List<TrainerBean> trainer;
    private List<LessonBean> lesson;
    private List<videolist> video;
    private TextView intro, phone;
    private ImageView jlbtp,fh,gz;
    private RecyclerView jl, kc, sp;
    private int id,uid;


    @Override
    public int getLayoutFile() {
        return R.layout.julebuxq;
    }

    @Override
    public void initView() {
        intro = (TextView) findViewById(R.id.intro);
        phone = (TextView) findViewById(R.id.phone);
        jlbtp = (ImageView) findViewById(R.id.jlbtp);
        fh=(ImageView)findViewById(R.id.fh);
        gz=(ImageView)findViewById(R.id.gz);
        jl = (RecyclerView) findViewById(R.id.jl);
        jl.setLayoutManager(new LinearLayoutManager(ClubActivity.this));
        kc = (RecyclerView) findViewById(R.id.kc);
        kc.setLayoutManager(new LinearLayoutManager(ClubActivity.this));
        sp = (RecyclerView) findViewById(R.id.sp);
        sp.setLayoutManager(new LinearLayoutManager(ClubActivity.this));


    }

    @Override
    public void initEvent() {
      fh.setOnClickListener(this);
      gz.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fh:
                finish();
                break;
            case R.id.gz:
                if(cbean.getFlag()==0){
                    getgz(id,uid);
                }else {
                    getqxgz(id,uid);
                }
                break;
        }
    }

    @Override
    public void initData() {

        uid=this.getSharedPreferences("User",this.MODE_PRIVATE).getInt("id",0);
        Intent intent=getIntent();
        int id=intent.getIntExtra("jid",0);
        getclubdetail(id,uid);
        getJl();
        getKc();
        getSp();


    }
    private void getclubdetail(int id,int uid){
        BaseModel<ClubDetailBean>model=new BaseModel<>();
        Call<ClubDetailBean>call=model.service.clubdetail(id,uid);
        model.callEnqueue(call,clubDetailBeanListener);
    }

    BaseListener<ClubDetailBean> clubDetailBeanListener=new BaseListener<ClubDetailBean>() {
        @Override
        public void onResponse(ClubDetailBean clubDetailBean1) {
            cbean=clubDetailBean1;
            intro.setText(clubDetailBean1.getCintro());
            phone.setText(clubDetailBean1.getCtel());
            id=clubDetailBean1.getCid();
            //根据Flag  修改 关注按钮 图片
            if (clubDetailBean1.getFlag()==1){
                gz.setImageResource(R.drawable.ic_gz1);
            }else {
                gz.setImageResource(R.drawable.ic_gz);
            }

            Picasso.with(ClubActivity.this).load("http://10.0.2.2/YueJian/public/static/img/clubimg/"
                    + clubDetailBean1.getCpic()).into(jlbtp);
        }

        @Override
        public void onFail(String msg) {
            showToast("获取俱乐部详情失败");
        }
    };

    private void getgz(int id,int uid){
        BaseModel<GuanzhuBean>model=new BaseModel<>();
        Call<GuanzhuBean>call=model.service.getguanzhu( id, uid);
        model.callEnqueue(call,list2);
    }
    BaseListener<GuanzhuBean>list2=new BaseListener<GuanzhuBean>() {
        @Override
        public void onResponse(GuanzhuBean guanzhuBean) {
            if(guanzhuBean.getStatus()==1){
                gz.setImageResource(R.drawable.ic_gz1);
                cbean.setFlag(1);
                showToast(guanzhuBean.getMessage());

            }else{
                showToast(guanzhuBean.getMessage());

            }
        }

        @Override
        public void onFail(String msg) {
                  showToast("关注失败");
        }
    };
    private void getqxgz(int id,int uid){
        BaseModel<CancelBean>model=new BaseModel<>();
        Call<CancelBean>call=model.service.getcancel( id, uid);
        model.callEnqueue(call,list3);
    }
    BaseListener<CancelBean>list3=new BaseListener<CancelBean>() {
        @Override
        public void onResponse(CancelBean cancelBean) {
            if(cancelBean.getStatus()==1){
                gz.setImageResource(R.drawable.ic_gz);
                cbean.setFlag(0);
                showToast(cancelBean.getMessage());

            }else{
                showToast(cancelBean.getMessage());

            }
        }

        @Override
        public void onFail(String msg) {
            showToast("取消关注失败");
        }
    };
    private void getJl() {
        BaseModel<TrainerBean> model = new BaseModel<>();
        Call<List<TrainerBean>> call = model.service.trainerlist();
        model.callListEnqueue(call, list);
    }


    BaseListener<List<TrainerBean>> list = new BaseListener<List<TrainerBean>>() {
        @Override
        public void onResponse(List<TrainerBean> trainerBeans) {
            trainer = trainerBeans;
            jl.setAdapter(new TrainerAdapter(trainer));
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private void getKc() {
        BaseModel<LessonBean> model = new BaseModel<>();
        Call<List<LessonBean>> call = model.service.lessonlist();
        model.callListEnqueue(call, list1);
    }

    BaseListener<List<LessonBean>> list1 = new BaseListener<List<LessonBean>>() {
        @Override
        public void onResponse(List<LessonBean> lessonBeans) {
            lesson = lessonBeans;
            kc.setAdapter(new LessonAdapter(lesson));
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private void getSp() {
        BaseModel<videolist> model = new BaseModel<>();
        Call<List<videolist>> call = model.service.videolist();
        model.callListEnqueue(call, list4);
    }

    BaseListener<List<videolist>> list4 = new BaseListener<List<videolist>>() {
        @Override
        public void onResponse(List<videolist> videolists) {
            video = videolists;
            sp.setAdapter(new VideoAdapter(video));
        }

        @Override
        public void onFail(String msg) {

        }
    };

    public class TrainerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private List<TrainerBean> trainerBean;
        private TrainerBean list;

        private TrainerAdapter(List<TrainerBean> mDataList) {

            this.trainerBean = mDataList;


        }

        @Override
        @NonNull
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(ClubActivity.this);
            View v = inflater.inflate(R.layout.jiaolian_item, parent, false);
            return new ViewHolder(v);
        }


        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
            list = trainerBean.get(position);
            if (null == list)
                return;
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.jl_tel.setText(list.getTtel());
            viewHolder.jl_jj.setText(list.getTintro());
            viewHolder.jl_mz.setText(list.getTname());
            Picasso.with(ClubActivity.this).load("http://10.0.2.2/YueJian/public/static/img/trainers/"
                    + list.getTpic()).into(viewHolder.jl_tp);
        }


        @Override
        public int getItemCount() {
            return trainerBean.size();
        }

        private class ViewHolder extends RecyclerView.ViewHolder {
            private ImageView jl_tp;
            private TextView jl_mz, jl_tel, jl_jj;


            private ViewHolder(View v) {
                super(v);
                jl_tp = v.findViewById(R.id.jl_tp);
                jl_mz = v.findViewById(R.id.jl_mz);
                jl_jj = v.findViewById(R.id.jl_jj);
                jl_tel = v.findViewById(R.id.jl_tel);

            }
        }
    }

    public class LessonAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private List<LessonBean> lessonBean;
        private LessonBean list;

        private LessonAdapter(List<LessonBean> mDataList) {

            this.lessonBean = mDataList;


        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(ClubActivity.this);
            View v = inflater.inflate(R.layout.kecheng_item, parent, false);
            return new ViewHolder(v);
        }


        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
            list = lessonBean.get(position);
            if (null == list)
                return;
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.kc_jg.setText(list.getLprice());
            viewHolder.kc_jj.setText(list.getLintro());
            viewHolder.kc_mz.setText(list.getLname());
        }

        @Override
        public int getItemCount() {
            return lessonBean.size();
        }

        private class ViewHolder extends RecyclerView.ViewHolder {
            private TextView kc_mz, kc_jg, kc_jj;
            private ViewHolder(View v) {
                super(v);
                kc_mz = v.findViewById(R.id.kc_mz);
                kc_jj = v.findViewById(R.id.kc_jj);
                kc_jg = v.findViewById(R.id.kc_jg);

            }
        }
    }

    public class VideoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private List<videolist> videolist;
        private videolist list;
        private MediaController mediaController;

        private VideoAdapter(List<videolist> mDataList) {

            this.videolist = mDataList;


        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(ClubActivity.this);
            View v = inflater.inflate(R.layout.video_item, parent, false);
            return new VideoHolder(v);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            list = videolist.get(position);
            if (null == list)
                return;
            VideoHolder videoHolder = (VideoHolder) holder;
            mediaController = new MediaController(ClubActivity.this);
            String url = "http://10.0.2.2/YueJian/public/static/video/" + list.getVfile();
            videoHolder.video.setVideoPath(url);
            videoHolder.video.setMediaController(mediaController);
            mediaController.setMediaPlayer(videoHolder.video);
            videoHolder.video.setFocusable(false);
//            videoHolder.video.requestFocus();
            //  videoHolder.video.start();
        }


        @Override
        public int getItemCount() {
            return videolist.size();
        }

        private class VideoHolder extends RecyclerView.ViewHolder {
            private VideoView video;

            private VideoHolder(View v) {
                super(v);
                video = v.findViewById(R.id.video);

            }
        }
    }
}